package com.itheima.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.pojo.Result;
import com.itheima.common.util.RequestContext;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.user.dto.UserRelationDto;
import com.itheima.user.pojo.ApUserFollow;
import com.itheima.user.service.ApUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* <p>
* APP用户关注信息表 控制器</p>
* @author 作者
* @since 2021-08-20
*/
@RestController
@RequestMapping("/apUserFollow")
public class ApUserFollowController extends AbstractCoreController<ApUserFollow> {

    private ApUserFollowService apUserFollowService;

    //注入
    @Autowired
    public ApUserFollowController(ApUserFollowService apUserFollowService) {
        super(apUserFollowService);
        this.apUserFollowService=apUserFollowService;
    }

    /**
     * 查询用户关注的集合中是否包含某个作者
     */
    @GetMapping(value = "/author/{authorId}") //作者的ID
    public Result<ApUserFollow> getByAuthorId(@PathVariable(value = "authorId")Integer authorId){
        //获取用户ID
        Integer userId = RequestContext.getNodeValue("id");
        QueryWrapper<ApUserFollow> queryWrapper = new QueryWrapper<ApUserFollow>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("follow_id",authorId);
        ApUserFollow follow = apUserFollowService.getOne(queryWrapper);
        //获取作者ID
        return Result.ok(follow);
    }


    /***
     * 关注/取关
     */
    @PostMapping(value = "/follow")
    public Result follow(@RequestBody UserRelationDto dto){
        //执行关注/取关操作
        apUserFollowService.follow(dto);
        return Result.ok();
    }
}

